﻿function OnFileTypesSelected(data) {
    $("#FileTypes").val(data.Object);
    $("#token").val('');
    $('#token').trigger('keyup');
    $('#FileTypesDialog').dialog('close');
}

function Copy(from, to) {
    $('#' + from + ' :selected').each(function (i, selected) {
        var element = $(selected).text();
        $('#' + to).append(new Option(element, element));
        $(this).remove();

    });
}

function SelectAllOptions() {
    var selObj = document.getElementById('SelectedTypes');
    for (var i = 0; i < selObj.options.length; i++) {
        selObj.options[i].selected = true;
    }
}

jQuery.fn.filterByText = function (textbox) {
    return this.each(function () {
        var select = this;
        var options = [];
        $(select).find('option').each(function () {
            options.push({ value: $(this).val(), text: $(this).text() });
        });
        $(select).data('options', options);

        $(textbox).bind('change keyup', function () {
            var options = $(select).empty().data('options');
            var search = $.trim($(this).val());
            var regex = new RegExp(search, "gi");

            $.each(options, function (i) {
                var option = options[i];
                if (option.text.match(regex) !== null && !optionExists(option.value)) {
                    $(select).append(
                        $('<option>').text(option.text).val(option.value)
                    );
                }
            });
        });
    });
};

function optionExists(val) {
    return $("#SelectedTypes option[value='" + val + "']").length !== 0;
}

$(function () {
    $('#OtherTypes').filterByText($('#token'));
});